package com.example.demo.controller;

import com.example.demo.entity.Env;
import com.example.demo.entity.ResponseInfo;
import com.example.demo.service.EnvService;
import com.example.demo.utils.PageInfoNew;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/env")
@Api(tags = "环境变量管理")
public class EnvController {
    @Resource
    EnvService envService;

    @PostMapping("/add")
    @ApiOperation(value = "新增环境变量")
    public ResponseInfo<String> insert(@RequestBody Env env) {
        envService.insert(env);
        return ResponseInfo.successInfo("");
    }

    @GetMapping("/getDetails")
    @ApiOperation(value = "获取环境变量详情")
    @ApiImplicitParam(name = "id", value = "环境变量id", required = true, dataType = "int")
    public ResponseInfo<Env> getDetails(@RequestParam int id) {
        return ResponseInfo.successInfo(envService.selectById(id));
    }

    @GetMapping("/page")
    @ApiOperation(value = "根据项目获取所有环境变量分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页长度", required = true, dataType = "int"),
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, dataType = "int"),
    })
    public ResponseInfo<PageInfoNew<Env>> EnvPage(@RequestParam int pageNum, @RequestParam int pageSize, @RequestParam int projectId) {
        return ResponseInfo.successInfo(envService.selectByProjectId(pageNum, pageSize, projectId));
    }

    @GetMapping("/list")
    @ApiOperation(value = "根据项目获取所有环境变量列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, dataType = "int"),
    })
    public ResponseInfo<List<Env>> list(@RequestParam int projectId) {
        return ResponseInfo.successInfo(envService.selectByProjectIdForList(projectId));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除环境接口")
    public ResponseInfo<String> deleteEnv(@PathVariable(value = "id") int id) {
        envService.deleteEnv(id);
        return ResponseInfo.successInfo("");
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新环境变量")
    public ResponseInfo<String> updateEnv(@RequestBody Env env) {
        envService.updateEnv(env);
        return ResponseInfo.successInfo("");
    }

}
